package com.xxx.blog.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.Article;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import com.xxx.blog.dto.ArticleTimeDTO;
import com.xxx.system.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Article")
@Api(tags = "博客文章类接口")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<Article>> finfAll(){
        List<Article> list = articleService.findAll();
        return R.ok(list);
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "ID查询单个")
    public R<Article> findOne(@PathVariable Long id) {
        Article Article = articleService.findOne(id);
        return R.ok(Article);
    }
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageInfo<Article> findByPage(@RequestBody ArticleQuery articleQuery) {
        List<Article> byPage = articleService.findByPage(articleQuery);
        PageInfo<Article> pageInfo = new PageInfo<>(byPage);
        return pageInfo;
    }

    @PutMapping
    @ApiOperation(value = "增加或者修改")
    @PreAuthorize(name = "新增文章" , sn="article:add")
    public R<Void> updateOrInsert(@RequestBody Article article){
        try {
            if (article.getId()!=null){
                articleService.updateById(article);
            }else {
                articleService.insert(article);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "单个删除")
    @PreAuthorize(name = "删除单篇文章", sn="article:delete")
    public R<Void> delete(@PathVariable Long id){
        articleService.deleteById(id);
        return R.ok();
    }


    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> bathArticles(@RequestBody Long[] ids) {
        try {
            articleService.bathArticles(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }
    @GetMapping("/gd")
    @ApiOperation(value = "根据时间归档")
    public R<List<ArticleTimeDTO>> articleTimeSta(){
        List<ArticleTimeDTO> list = articleService.articleTimeSta();
        return R.ok(list);
    }

    @GetMapping("/ra/{id}")
    @ApiOperation(value = "查询所有")
    public R<List<Article>> getRA(@PathVariable Long id){
        List<Article> list = articleService.getRA(id);
        return R.ok(list);
    }

    @GetMapping("/getArticlesByTypeID/{id}")
    @ApiOperation(value = "查询所有文章根据文章类型id")
    public R<List<Article>> getArticlesByTypeID(@PathVariable Long id){
        List<Article> list = articleService.getArticlesByTypeID(id);
        return R.ok(list);
    }

    @GetMapping("/gdt/{time}")
    @ApiOperation(value = "根据时间归档")
    public R<List<ArticleTimeDTO>> articleTimeStaByTime(@PathVariable String time){
        List<ArticleTimeDTO> list = articleService.articleTimeStaByTime(time);
        return R.ok(list);
    }



}
